/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import jpa.controller.ClienteJpaController;
import jpa.entity.Cliente;

/**
 *
 * @author isaias
 */
@ManagedBean
@RequestScoped
public class ClienteBean implements Serializable{

    private String filterNameCliente;
    private Cliente cliente = new Cliente();
    private Cliente clienteSelected = new Cliente();

    /** Creates a new instance of ClienteBean */
    public ClienteBean() {
    }

    public void save(){
        try {
            if(cliente != null){
                if(cliente.getCidade() != null){
                   new ClienteJpaController().create(cliente);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Cliente criado com sucesso!", null));
                }else{
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "O campo cidade precisa ser preenchido!", null));
                }
            }
        } catch (Exception e) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Erro ao tentar criar cliente!", null));
                e.printStackTrace();
        }finally{
            cliente = new Cliente();
        }
    }

    public List<Cliente> getListClients(){
        return new ClienteJpaController().findClienteEntities();
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public Cliente getClienteSelected() {
        return clienteSelected;
    }

    public void setClienteSelected(Cliente clienteSelected) {
        this.clienteSelected = clienteSelected;
    }

    public String getFilterNameCliente() {
        return filterNameCliente;
    }

    public void setFilterNameCliente(String filterNameCliente) {
        this.filterNameCliente = filterNameCliente;
    }

}
